package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.wogreat.WogreatAccountType;
import com.sls.web.manager.game.pojo.wogreat.WogreatAreaServer;
import com.sls.web.manager.game.pojo.wogreat.WogreatProduct;
import com.sls.web.manager.game.pojo.wogreat.WogreatRechargeType;
import com.sls.web.manager.game.pojo.wogreat.WogreatRespInfo;
import com.sls.web.manager.game.service.WogreatService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Service("wogreatService")
public class WogreatServiceImpl extends SuperService implements WogreatService {

	private Gson gson;

	public WogreatServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public Pagination<WogreatProduct> queryProPagination(
			WogreatProduct proQuery, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020965");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, proQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<WogreatProduct> page = gson.fromJson(body,
					new TypeToken<Pagination<WogreatProduct>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<WogreatAccountType> queryAccPagination(
			WogreatAccountType accQuery, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020966");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, accQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<WogreatAccountType> page = gson.fromJson(body,
					new TypeToken<Pagination<WogreatAccountType>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<WogreatRechargeType> queryRechPagination(
			WogreatRechargeType rechQuery, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020967");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, rechQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<WogreatRechargeType> page = gson.fromJson(body,
					new TypeToken<Pagination<WogreatRechargeType>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<WogreatAreaServer> queryASPagination(
			WogreatAreaServer asQuery, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020968");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, asQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<WogreatAreaServer> page = gson.fromJson(body,
					new TypeToken<Pagination<WogreatAreaServer>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public WogreatProduct queryById(String id, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020964");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, id);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				WogreatProduct pro = gson.fromJson(body, WogreatProduct.class);
				return pro;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void updatePro(WogreatProduct pro, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020963");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, pro);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void sync(UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020960");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void confirmAudit(String supplierId, int priceRatio,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020972");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setSupplierId(supplierId);
		wp.setPriceRatio(priceRatio);
		wp.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOnline(String state, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020973");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setOperator(userInfoVO.getUserName());
		wp.setStatus(state);
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOffline(String state, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020973");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setOperator(userInfoVO.getUserName());
		wp.setStatus(state);
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public List<WogreatProduct> queryAllProduct(UserInfoVO userInfoVO) {
		WogreatProduct query = new WogreatProduct();
		query.setCurrentPage(1);
		query.setPageSize(20);
		List<WogreatProduct> spList = new LinkedList<WogreatProduct>();
		Pagination<WogreatProduct> page = this.queryProPagination(query,
				userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				spList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (query.getPageSize() == 0) {
				query.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
				query.setCurrentPage(i + 1);
				page = this.queryProPagination(query, userInfoVO);
				if (page.getList() != null) {
					spList.addAll(page.getList());
				}
			}
			return spList;
		}
		return null;
	}

	@Override
	public WogreatRespInfo queryBalance(UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020959");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				WogreatRespInfo respInfo = gson.fromJson(body,
						WogreatRespInfo.class);
				return respInfo;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public WogreatRespInfo queryOrder(String orderId, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020961");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatRespInfo info = new WogreatRespInfo();
		info.setOrderId(orderId);
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				WogreatRespInfo respInfo = gson.fromJson(body,
						WogreatRespInfo.class);
				return respInfo;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void save(WogreatProduct pro, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021007");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, pro);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

}
